package com.xiaobaibai.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobaibai.common.EsCommon;
import com.xiaobaibai.dao.ProductDao;
import com.xiaobaibai.entity.OrderItem;
import com.xiaobaibai.entity.Product;
import com.xiaobaibai.service.IOrderItemService;
import com.xiaobaibai.service.IProductService;
import com.xiaobaibai.vo.CreateOrderItemVo;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2020-10-16
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements IProductService {

    @Autowired
    private RestHighLevelClient esClient;

    @Autowired
    private IOrderItemService orderItemService;

    @Override
    public List<Integer> searchByEs(String productName) {
        SearchRequest searchRequest=new SearchRequest(EsCommon.PRODUCTINDEX);

        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(
                QueryBuilders.matchQuery("productName",productName));

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = null;
        try {
            search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Integer> list=new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            list.add(Integer.parseInt(hit.getId()));
        }
        return list;
    }

    @Override//后面做客户端时就来个缓存
    public List<String> associatedWords(String font) {

        SearchRequest searchRequest = new SearchRequest(EsCommon.PRODUCTINDEX);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("productName", font);

        searchSourceBuilder.query(matchQueryBuilder).fetchSource("productName",null);

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = null;
        try {
            search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(search==null)
            return new ArrayList<>();

        SearchHit[] hits = search.getHits().getHits();

        List<String> list=new ArrayList<>(hits.length);

        for (SearchHit hit : hits) {
            String resultName =(String) hit.getSourceAsMap().get("productName");
            list.add(resultName);
        }

        return list;
    }

    @Override
    public boolean haveProductByCategoryId(Integer categoryId) {
        return this.baseMapper.haveProductByCategoryId(categoryId)>0;
    }

    @Override
    public List<Product> getProductByList(Collection collection) {
        return this.baseMapper.getProductByList(collection);
    }

    @Override
    public void returnStock(String orderId) {
        QueryWrapper<OrderItem> q1=new QueryWrapper<>();
        q1.select("product_id","product_count");
        q1.eq("order_id",orderId);
        List<OrderItem> orderItemList = orderItemService.list(q1);
        List<CreateOrderItemVo> list=new ArrayList<>(orderItemList.size());
        orderItemList.forEach(o->{
            CreateOrderItemVo createOrderItemVo=new CreateOrderItemVo();
            createOrderItemVo.setCount(o.getProductCount());
            createOrderItemVo.setProductId(o.getProductId());
            list.add(createOrderItemVo);
        });
        //返还库存
        this.baseMapper.returnStock(list);
    }

}
